﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint.Explorer;
using SPVSX.Explorer.Extensions.CommandIds;
using SPVSX.Explorer.Extensions.NodeInformation;
using SPVSX.Explorer.Extensions.Properties;

namespace SPVSX.Explorer.Extensions
{
    /// <summary>
    /// Represents an extension of SharePoint site nodes in Server Explorer for hide custom actions. 
    /// </summary>
    // Enables Visual Studio to discover and load this extension.
    [Export(typeof(IExplorerNodeTypeExtension))]
    // Indicates that this class extends SharePoint site nodes in Server Explorer.
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    class HideCustomActionNodeSiteExtension : IExplorerNodeTypeExtension
    {
        #region Methods

        /// <summary>
        /// The NodeChildrenRequested event is raised when the user expands the SharePoint site node in Server Explorer.
        /// </summary>
        /// <param name="nodeType">The node type</param>
        public void Initialize(IExplorerNodeType nodeType)
        {
            //Bind the events
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
        }

        /// <summary>
        /// Creates the new Hide Custom Actions node with the specified icon.
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The ExplorerNodeEventArgs object</param>
        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            // The CreateCustomActionNodes argument is a delegate that Visual Studio calls 
            // to create the child nodes under the Hide Custom Actions node.
            GenericFolderNode.Create(e.Node, "Hide Custom Actions",
                Resources.HideCustomActionsNode.ToBitmap(), 
                CreateHideCustomActionNodes);
        }

        /// <summary>
        /// Creates all of the individual hide custom action nodes under the new hide custom actions node.
        /// </summary>
        /// <param name="parentNode">The parent node</param>
        private void CreateHideCustomActionNodes(IExplorerNode parentNode)
        {
            // Call the custom SharePoint command to get items from the hide custom actions.
            var hideCustomActions = 
                parentNode.Context.SharePointConnection.ExecuteCommand<HideCustomActionNodeInfo[]>(HideCustomActionCommandIds.GetHideCustomActions);

            if (hideCustomActions != null)
            {
                foreach (HideCustomActionNodeInfo hideCustomAction in hideCustomActions)
                {
                    // Create a new annotation object to store the current custom action item with the new node.
                    var annotations = new Dictionary<object, object>() 
                    { 
                        { typeof(HideCustomActionNodeInfo), hideCustomAction } 
                    };

                    // Create the new node for the current HideCustomAction item.
                    parentNode.ChildNodes.Add(HideCustomActionNodeTypeProvider.HideCustomActionNodeTypeId,
                        hideCustomAction.Name, 
                        annotations);
                }
            }
        }

        #endregion

    }
}
